import {storage, tenantName} from '@/config'
import cookie from 'js-cookie'

/**
 * @description 获取租户相关
 * @returns {string|ActiveX.IXMLDOMNode|Promise<any>|any|IDBRequest<any>|MediaKeyStatus|FormDataEntryValue|Function|Promise<Credential | null>}
 */
export function getTenantId() {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.getItem(tenantName)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.getItem(tenantName)
    } else if ('cookie' === storage) {
      return cookie.get(tenantName)
    } else {
      return localStorage.getItem(tenantName)
    }
  } else {
    return localStorage.getItem(tenantName)
  }
}

/**
 * @description 存储租户相关
 * @param token
 * @returns {void|*}
 */
export function setTenantId(token: string) {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.setItem(tenantName, token)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.setItem(tenantName, token)
    } else if ('cookie' === storage) {
      return cookie.set(tenantName, token)
    } else {
      return localStorage.setItem(tenantName, token)
    }
  } else {
    return localStorage.setItem(tenantName, token)
  }
}

/**
 * @description 移除租户相关
 * @returns {void|Promise<void>}
 */
export function removeTenantId() {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.removeItem(tenantName)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.clear()
    } else if ('cookie' === storage) {
      return cookie.remove(tenantName)
    } else {
      return localStorage.removeItem(tenantName)
    }
  } else {
    return localStorage.removeItem(tenantName)
  }
}
